package com.xian.hadoop.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
//导包是org.apache.hadoop.mapreduce，而不是org.apache.hadoop.mapred.Mapper这是老版本
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;


/**
 * Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> 四个泛型类
 * KEYIN  Long值 一行偏移量    org.apache.hadoop.io.LongWritable
 * VALUEIN 读到的一行文本内容 String值  org.apache.hadoop.io.Text
 * KEYOUT  自定义输出的单词   String值  org.apache.hadoop.io.Text
 * VALUEOUT  自定义的单词数   Int值  org.apache.hadoop.io.IntWritable
 * @author xian
 *
 */
public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private final static IntWritable one = new IntWritable(1); //计数器
    private Text word = new Text(); //封装每个词

    //每遍历一行调用一次这个map方法
    @Override
    protected void map(LongWritable key, Text line, Context context) throws IOException, InterruptedException {
        String[] words = line.toString().split(" ");
        Text t_word = new Text();
        IntWritable t_one = new IntWritable();
        for (String word : words) {
            t_word.set(word);
            t_one.set(1);
            context.write(t_word, t_one);
        }

    }
}
